package com.by.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.by.goods.mapper.ApGoodsMapper;
import com.by.goods.service.ApGoodsService;
import com.by.model.common.dtos.PageResponseResult;
import com.by.model.common.dtos.ResponseResult;
import com.by.model.goods.pojo.ApGoods;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApGoodsServiceImpl extends com.baomidou.mybatisplus.extension.service.impl.ServiceImpl<ApGoodsMapper, ApGoods> implements ApGoodsService {
    @Override
    public ResponseResult add(ApGoods apGoods) {
       apGoods.setStatus(3);
        boolean save = save(apGoods);
        if (save){
            return ResponseResult.okResult("添加成功")  ;
        }
        return ResponseResult.errorResult(1,"添加失败" );
    }

    @Override
    public ResponseResult del(ApGoods apGoods) {
        if (apGoods.getStatus()==3){
            removeById(apGoods.getId());
        }else {
            return ResponseResult.errorResult(1,"商品不可删除" );
        }
        return ResponseResult.okResult("删除成功");
    }

    @Override
    public ResponseResult update(ApGoods apGoods) {
        if (apGoods.getStatus()==3||apGoods.getStatus()==5||apGoods.getStatus()==7){
            boolean update = updateById(apGoods);
            if (update){
                return ResponseResult.okResult("修改成功");
            }
            return ResponseResult.errorResult(1,"修改失败");
        }
        return ResponseResult.errorResult(1,"权限不足" );
    }

    @Override
    public PageResponseResult show() {
        QueryWrapper<ApGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ApGoods::getStatus, 2).or().eq(ApGoods::getStatus,  9).or().eq(ApGoods::getStatus,  5);

        Page p = new Page(1, 10);
        IPage page = page(p, queryWrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(1, 10, (int) page.getTotal());
        pageResponseResult.setCode(0);
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult status(ApGoods apGoods) {
        boolean update = update(apGoods, Wrappers.<ApGoods>lambdaUpdate().eq(ApGoods::getId,apGoods.getId() ).set(ApGoods::getStatus, apGoods.getStatus()));
        if (update){
            return ResponseResult.okResult("修改成功");
        }
        return ResponseResult.okResult("修改失败");
    }
}
